package com.cmf.runex.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.scripting.xmltags.IfSqlNode;
import org.junit.Test;
import org.springframework.stereotype.Service;

import com.cmf.runex.constant.CommoneConstant;
import com.cmf.runex.controller.AndroidExController;
import com.cmf.runex.dao.AndroidExDao;
import com.cmf.runex.domain.AndroidEx;

@Service
public class AndroidExServerImpl implements AndroidExService {

	@Resource
	private AndroidExDao androidExDao;

	public void updateAndroidEx(AndroidEx androidEx) {
		androidExDao.updateAndroidEx(androidEx);
	}

	public List<AndroidEx> getAndroidExList() {
		return androidExDao.getAndroidExList();
	}

	public AndroidEx checkAndroidEx(AndroidEx androidEx, HttpServletRequest request) {

		// 1。检查是否存在相同的对象。
		// 2.如果有相同的，就默认把开关设置为关闭，返回数据。
		// 3.如果没有相同，就直接插入新数据，并且把开关设置为关闭。返回数据。
		// 4.根据Cmfcomm模板，替换对应的Cmfcomm文件，提供下载地址。

		AndroidEx aex = getOneAndroidEx(androidEx.getClientId());
		if (aex == null) {
			androidEx.setSwitch_(-1);
			addAndroidEx(androidEx);

			aex = androidEx;
		}

		String realPath = aex.getClientId() + "_" + aex.getPhoneModle();
		String fileName = "cmfCommon.js";
		String path1 = request.getSession().getServletContext().getRealPath(realPath) + File.separator;

		File localFile = new File(path1);
		if (!localFile.exists()) {
			localFile.mkdirs();
		}
		File oldFile = new File(request.getSession().getServletContext().getRealPath("js") + "/cmfCommon.js");

		switch (aex.getAppSetting()) {

		case 1:
			updateCommFile(CommoneConstant.pro_appurl, CommoneConstant.pro_commonUrl, oldFile, path1 + fileName);
			break;

		case 2:
			updateCommFile(CommoneConstant.pre_appurl, CommoneConstant.pre_commonUrl, oldFile, path1 + fileName);
			break;

		case 3:
			updateCommFile(CommoneConstant.test_appurl, CommoneConstant.test_commonUrl, oldFile, path1 + fileName);
			break;

		default:
			break;
		}
		
//		aex.setCmfCommonUrl(
//				"http://192.168.8.117:8080/ssm_project/download?fileName=" + fileName + "&clientId=" + aex.getClientId());
		
		aex.setCmfCommonUrl(
				"http://pro.cconon.com:8080/ssm_project/download?fileName=" + fileName + "&clientId=" + aex.getClientId());
		updateAndroidEx(aex);
		return aex;
	}

	public AndroidEx getOneAndroidEx(String clientId) {
		return androidExDao.getOneAndroidEx(clientId);
	}

	public int addAndroidEx(AndroidEx androidEx) {

		AndroidEx aex = getOneAndroidEx(androidEx.getClientId());

		if (aex == null) {
			androidExDao.addAndroidEx(androidEx);
		} else {
			return 2;
		}

		aex = getOneAndroidEx(androidEx.getClientId());

		if (aex == null) {
			return -1;
		}
		return 1;
	}

	public void updateCommFile(String appurl, String commUrl, File oldFile, String domurl) {
		File file = oldFile;

		try {
			BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));// 数据流读取文件

			StringBuffer strBuffer = new StringBuffer();
			String empty = "";
			String tihuan = "";
			for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {

				if (temp.indexOf("#ex_appUrl") != -1) {

					tihuan = temp.substring(temp.indexOf("#ex_appUrl"), temp.indexOf("\";"));
					temp = temp.replace(tihuan, appurl);

				} else if (temp.indexOf("#ex_commonUrl") != -1) {

					tihuan = temp.substring(temp.indexOf("#ex_commonUrl"), temp.indexOf("\";"));
					temp = temp.replace(tihuan, commUrl);
				}

				strBuffer.append(temp);
				strBuffer.append(System.getProperty("line.separator"));// 行与行之间的分割
			}
			bufReader.close();
			PrintWriter printWriter = new PrintWriter(domurl);// 替换后输出的文件位置
			printWriter.write(strBuffer.toString().toCharArray());
			printWriter.flush();
			printWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
